package org.example.study11.condition;

public class MultiConditionTest {
    public static void main(String[] args) {
        ResourceController controller = new ResourceController();

        // 创建资源A的读写线程
        Thread writerA = new Thread(() -> {
            try {
                for (int i = 1; i <= 3; i++) {
                    controller.writeToA(i * 10); // 写入资源A
                    Thread.sleep(100);
                }
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        }, "Writer-A");

        Thread readerA = new Thread(() -> {
            try {
                for (int i = 1; i <= 3; i++) {
                    controller.readFromA(); // 读取资源A
                    Thread.sleep(150);
                }
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        }, "Reader-A");

        // 创建资源B的读写线程
        Thread writerB = new Thread(() -> {
            try {
                for (int i = 1; i <= 3; i++) {
                    controller.writeToB(i * 100); // 写入资源B
                    Thread.sleep(120);
                }
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        }, "Writer-B");

        Thread readerB = new Thread(() -> {
            try {
                for (int i = 1; i <= 3; i++) {
                    controller.readFromB(); // 读取资源B
                    Thread.sleep(180);
                }
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        }, "Reader-B");

        // 启动所有线程
        readerA.start();    // 启动资源A的读取者
        readerB.start();    // 启动资源B的读取者
        writerA.start();    // 启动资源A的写入者
        writerB.start();    // 启动资源B的写入者
    }
}
